import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "../views/Layout.vue";

const router = createRouter({
  history: createWebHashHistory("/blog"),
  routes: [
    {
      path: "/",
      redirect: "/index",
      component: Layout,
      children: [
        {
          path: "/index",
          name: "index",
          component: () => import("../views/Home/index.vue"),
        },
        {
          path: "/category",
          name: "category",
          component: () => import("../views/Category/category.vue"),
        },
        {
          path: "/item/:title/:id",
          name: "blogItem",
          component: () => import("../views/Details/detail.vue"),
        },
        {
          path: "/search",
          name: "search",
          component: () => import("../views/Search/search.vue"),
        },
        {
          path: "/archiving",
          name: "archiving",
          component: () => import("@/views/Archiving/archiving.vue"),
        },
        {
          path: "/about",
          name: "about",
          component: () => import("../views/About/about.vue"),
        },
        {
          path: "/tag-article/:name/:id",
          name: "tagArticle",
          component: () => import("../views/TagArticle/tagArticle.vue"),
        },
        // 未匹配
        {
          path: "/:pathMatch(.*)*",
          name: "404",
          component: () => import("../components/common/NotFound.vue"),
        },
        {
          path: "/404",
          name: "404",
          component: () => import("../components/common/NotFound.vue"),
        },
      ],
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
});

export default router;
